package com.mmp.main.servlet.joborder;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class JobOrderTrackingDisplayServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		JobOrder jo = (JobOrder) request.getAttribute(Default.JOB_ORDER_INFO_ATTR_NAME);
		String dest = null;

		if (!jo.getClosed() && !jo.getCancelled()) {

			String reqPhaseId = (String) request.getAttribute(Default.JOBORDER_PHASE_ATTR_NAME);

			// previous phase required
			if (reqPhaseId != null) {

				if (WebUtil.userHasRightToUpdateCompletedPhasesOfJobOrderFollowup(request)) {

					int requiredPhaseId = Integer.parseInt(reqPhaseId);

					if (requiredPhaseId == Enums.JOBORDER_FOLLOWUP_PHASES.FIRST_ACTION_TAKEN.getId())
						dest = "JobOrderFollowupFirstPhase.jsp";
//					else if (WebUtil.jobOrderPhaseIsPartOfMiddlePhases(requiredPhaseId))
//						dest = "JobOrderFollowupSecondPhase.jsp";

				}
				// next phase required
			} else {

				int joCurrentPhase = jo.getLastCompletedPhaseId();

				if (joCurrentPhase == Enums.JOBORDER_FOLLOWUP_PHASES.NOT_HANDLED_YET.getId())
					dest = "JobOrderFollowupFirstPhase.jsp";
				else if (joCurrentPhase == Enums.JOBORDER_FOLLOWUP_PHASES.FIRST_ACTION_TAKEN.getId()
						|| WebUtil.jobOrderPhaseIsPartOfMiddlePhases(joCurrentPhase))
					dest = "JobOrderFollowupSecondPhase.jsp";
				else if (joCurrentPhase == Enums.JOBORDER_FOLLOWUP_PHASES.SECOND_ACTION_TAKEN.getId()
						&& jo.getSpSource() == Enums.JOBORDER_SP_SOURCES.AGENT.getId())
					dest = "JobOrderFollowupThirdPhase.jsp";
			}
		}

		if (dest != null)
			forward(request, response, "joborder/" + dest);
		else
			announceErorrMessage(request, response, "عفوا .. لا يمكنك طلب هذا المحتوى");

	}
}
